﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using Telerik.Windows;
using System.Diagnostics;
using System.IO;

namespace Abhiruchi_Phase1
{
    /// <summary>
    /// Interaction logic for UC_HRM_EmpSearch.xaml
    /// </summary>
    public partial class UC_HRM_EmpSearch : UserControl
    {        
        public string Photo, FamilyPhoto;
        public UC_HRM_EmpSearch()
        {
            InitializeComponent();            
            LoadEmptyGrid();
            btnEditEmp.Visibility = Visibility.Collapsed;
        }
        public static string Finalpath = "";
        public static string Finalpath1 = "";
        private void radGridView1_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {

            if (grdEmp.SelectionMode == SelectionMode.Single)
            {
                DataTable mydtPersonal = SearchEmployeeDAL.LoadAllEmpDataFromDB(5, ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString());
                DataTable mydtOrg = SearchEmployeeDAL.LoadAllEmpDataFromDB(6, ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString());
                DataTable mydtComm = SearchEmployeeDAL.LoadAllEmpDataFromDB(7, ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString());
                if (mydtPersonal.Rows.Count > 0)
                {
                    for (int i = 0; i < mydtPersonal.Rows.Count; i++)
                    {
                        if (mydtPersonal.Rows[i][0].ToString() == ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString())
                        {
                            lblPFnameRes.Text = mydtPersonal.Rows[i][0].ToString();
                            lblPDOBRes.Text = mydtPersonal.Rows[i][1].ToString();
                            lblPStatusRes.Text = mydtPersonal.Rows[i][2].ToString();
                            string path = mydtPersonal.Rows[i][4].ToString();
                            Finalpath = path;
                            try
                            {
                                if (path != "" && path != null)
                                {
                                    BitmapImage src = new BitmapImage();
                                    src.BeginInit();
                                    src.UriSource = new Uri(path.Trim(), UriKind.Absolute);
                                    src.CacheOption = BitmapCacheOption.OnLoad;
                                    src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                                    src.EndInit();
                                    ShowImg.Visibility = Visibility.Visible;
                                    ShowImg.Source = src;
                                }
                                else
                                {
                                    BitmapImage src = new BitmapImage();
                                    src.BeginInit();
                                    src.UriSource = new Uri(getPathofFile() + @"\Images\photoD.png", UriKind.Relative);
                                    src.CacheOption = BitmapCacheOption.OnLoad;
                                    src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                                    src.EndInit();
                                    ShowImg.Source = src;
                                    ShowImg.Visibility = Visibility.Visible;
                                }
                                string path1 = mydtPersonal.Rows[i][5].ToString();
                                Finalpath1 = path1;
                                if (path1 != "" && path != null)
                                {
                                    BitmapImage src1 = new BitmapImage();
                                    src1.BeginInit();
                                    src1.UriSource = new Uri(path1.Trim(), UriKind.Absolute);
                                    src1.CacheOption = BitmapCacheOption.OnLoad;
                                    src1.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                                    src1.EndInit();
                                    imgFamily.Visibility = Visibility.Visible;
                                    imgFamily.Source = src1;
                                }
                                else
                                {
                                    BitmapImage src = new BitmapImage();
                                    src.BeginInit();
                                    src.UriSource = new Uri(getPathofFile() + @"\Images\family.png", UriKind.Relative);
                                    src.CacheOption = BitmapCacheOption.OnLoad;
                                    src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
                                    src.EndInit();
                                    imgFamily.Source = src;
                                    imgFamily.Visibility = Visibility.Visible;
                                }
                            }

                            catch (Exception ex)
                            {
                            }
                        }
                    }
                }

                if (mydtOrg.Rows.Count > 0)
                {
                    for (int i = 0; i < mydtOrg.Rows.Count; i++)
                    {
                        if (mydtOrg.Rows[i][0].ToString() == ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString())
                        {
                            lblODOJRes.Text = mydtOrg.Rows[i][1].ToString();
                            lblODesRes.Text = mydtOrg.Rows[i][2].ToString();
                            lblORepToRes.Text = mydtOrg.Rows[i][3].ToString();
                        }
                    }
                }
                if (mydtComm.Rows.Count > 0)
                {
                    for (int i = 0; i < mydtComm.Rows.Count; i++)
                    {
                        if (mydtComm.Rows[i][0].ToString() == ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString())
                        {
                            lblCAddressRes.Text = mydtComm.Rows[i][1].ToString() + ", " + mydtComm.Rows[i][2].ToString() + ", " + mydtComm.Rows[i][3].ToString() + ", \n" + mydtComm.Rows[i][4].ToString() + ", " + mydtComm.Rows[i][5].ToString() + ", " + mydtComm.Rows[i][6].ToString();
                            lblCMbWRes.Text = mydtComm.Rows[i][7].ToString();
                            lblCMbHmRes.Text = mydtComm.Rows[i][8].ToString();
                            lblCLndWkRes.Text = mydtComm.Rows[i][9].ToString();
                            lblCLndHomeRes.Text = mydtComm.Rows[i][10].ToString();
                            lblCPEmRes.Text = mydtComm.Rows[i][11].ToString();
                            lblCOfEmRes.Text = mydtComm.Rows[i][12].ToString();
                        }
                    }
                }
            }

        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            grdEmp.ItemsSource = null;
            DataTable mydt = loadDataintoTable();            

            if (cmbEType.Text == "Permanent Employee")
            {
                for (int i = 0; i < mydt.Rows.Count; i++)
                {
                    if (mydt.Rows[0][1].ToString().Contains("CE"))
                    {
                        mydt.Rows.Remove(mydt.Rows[0]);
                    }
                }
            }
            else if (cmbEType.Text == "Contract Employee")
            {
                for (int i = 0; i < mydt.Rows.Count; i++)
                {
                    if (mydt.Rows[i][1].ToString().Contains("PE"))
                    {
                        mydt.Rows.Remove(mydt.Rows[i]);
                        i--;                      
                    }
                }
            }                                
            grdEmp.ItemsSource = mydt;
        }
        public void LoadEmptyGrid()
        {
            DataTable mydtTask = new DataTable();
            DataRow dtrow;
            for (int i = 0; i < 10; i++)
            {
                dtrow = mydtTask.NewRow();
                mydtTask.Rows.Add(dtrow);
            }
            grdEmp.ItemsSource = mydtTask;
        }

        private DataTable loadDataintoTable()
        {            
            DataTable mydt = new DataTable();
            mydt.Clear();           

            if (txtEName.Text == "" && txtENo.Text == "")
            {
                mydt = SearchEmployeeDAL.LoadAllEmpDataFromDB(1);
            }
            else
                if (txtEName.Text != "" && txtENo.Text == "")
                {
                    mydt = SearchEmployeeDAL.LoadAllEmpDataFromDB(txtEName.Text, 2);
                }
                else
                    if (txtENo.Text != "" && txtEName.Text == "")
                    {
                        mydt = SearchEmployeeDAL.LoadAllEmpDataFromDB(3, txtENo.Text);
                    }
                    else
                    {
                        mydt = SearchEmployeeDAL.LoadAllEmpDataFromDB(4, txtEName.Text, txtENo.Text);
                    }          
            return mydt;
        }
        private void btnClrSearch_Click(object sender, RoutedEventArgs e)
        {
            cmbEType.SelectedIndex = 0;
            txtEName.Text = "";
            txtENo.Text = "";
            grdEmp.ItemsSource = null;
            LoadEmptyGrid();
            lblPFnameRes.Text = "";
            lblPDOBRes.Text = "";
            lblPStatusRes.Text = "";
            lblODOJRes.Text = "";
            lblODesRes.Text = "";
            lblORepToRes.Text = "";
            lblCAddressRes.Text = "";
            lblCMbWRes.Text = "";
            lblCMbHmRes.Text = "";
            lblCLndWkRes.Text = "";
            lblCLndHomeRes.Text = "";
            lblCPEmRes.Text = "";
            lblCOfEmRes.Text = "";
            BitmapImage src = new BitmapImage();
            src.BeginInit();
            src.UriSource = new Uri(getPathofFile() + @"\Images\photoD.png", UriKind.Relative);
            src.CacheOption = BitmapCacheOption.OnLoad;
            src.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            src.EndInit();
            ShowImg.Source = src;
            ShowImg.Visibility = Visibility.Visible;
            BitmapImage src1 = new BitmapImage();
            src1.BeginInit();
            src1.UriSource = new Uri(getPathofFile() + @"\Images\family.png", UriKind.Relative);
            src1.CacheOption = BitmapCacheOption.OnLoad;
            src1.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            src1.EndInit();
            imgFamily.Source = src1;
            imgFamily.Visibility = Visibility.Visible;
        }

        private void btnEditEmp_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnDispPhoto_Click(object sender, RoutedEventArgs e)
        {
            if (Finalpath != "")
            {
                System.Diagnostics.Process.Start(Finalpath);
            }
            else
            {
                MessageBox.Show("No Photo Available for this Employee", "Photo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
        private string getPathofFile()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string[] pathArray = path.Split(new char[] { '\\' });
            path = string.Empty;
            for (int temp = 0; temp < pathArray.Count() - 3; temp++)
            {
                if (temp == 0)
                    path = path + pathArray[temp];
                else
                    path = path + "\\" + pathArray[temp];
            }

            return path;
        }

        private void btnFamilyView_Click(object sender, RoutedEventArgs e)
        {    
            if(Finalpath1!="")
            {
            System.Diagnostics.Process.Start(Finalpath1);
            }
            else
            {
                MessageBox.Show("No family Photo Available for this Employee", "Family Photo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
       
    }
}
    